var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');
var app = getApp();
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page(common.extendPage(MPage, {
    data: {
        time: 0,
        curSection: 'wx',

        tabs: ["一键绑定", "手动绑定"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        sliderWidth,
        userInfo: {}
    },

    onLoad: function(options) {
        // this._initTabs();
        var _self = this;
        app.getUserInfo(function(userInfo) {
            _self.setData({
                userInfo
            })
        });
        common.initApp();
        // this._loginBack(1);
    },

    _switchTap(e) {
        var {
            to
        } = e.target.dataset;

        this.setData({
            curSection: to
        });

    },

    // _initTabs() {
    //     var that = this;

    //     wx.getSystemInfo({
    //         success: function(res) {
    //             that.setData({
    //                 sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
    //                 sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
    //             });
    //         }
    //     });
    // },

    // tabClick: function(e) {
    //     var id = e.currentTarget.id;
    //     var formId = e.detail.formId;

    //     this.setData({
    //         sliderOffset: e.currentTarget.offsetLeft,
    //         activeIndex: id
    //     });
    //     common.submitForm(formId);
    // },

    //输入手机号
    _phoneInput(e) {
        this._phone = e.detail.value;
    },

    //获取验证码
    _getCode() {
        var phone = this._phone;

        if (!phone) {
            return util.alert('请输入手机号');
        }

        ajax.P('/openapi/CBrand/sendMsg2', {
            phone: phone,
        }, function(data) {

            this.setData({
                time: 60
            });
            this._timerDown();

        }.bind(this));

    },

    //授权微信绑定手机号
    _loginByPhoneNumber(e) {
        var {
            errMsg,
            iv,
            encryptedData
        } = e.detail;
        var {
            nickName,
            avatarUrl
        } = app.globalData.userInfo || {};

        if (!/:ok$/.test(e.detail.errMsg)) {
            return;
        }
        ajax.P('/openapi/CWeixinSdk/getUserPhone', {
            iv,
            encryptedData,
            nickName,
            avatarUrl
        }, function(data) {
            this._loginSuc(data);
        }.bind(this));
    },

    //登录
    _login(e) {
        var _self = this;
        var formId = e.detail.formId;
        var params = e.detail.value || {};
        var {
            nickName,
            avatarUrl
        } = app.globalData.userInfo;

        params.openId = app.getOpenId();
        params.nickName = nickName;
        params.avatarUrl = avatarUrl;

        ajax.P('/openapi/CBrand/bandPhone2', params, function(data) {
            _self._loginSuc(data);
        });
        common.submitForm(formId);
    },

    //登陆成功
    _loginSuc(data) {
        var userSid = data.result.userSid;
        var is_fenxiao = data.result.is_fenxiao;
        var user_id = data.result.user_id;

        app.setUserSid(userSid);
        app.setIsFenxiao(is_fenxiao);
        app.setUserId(user_id);
        wx.navigateBack();
        // this._loginBack(0);
    },

    //启动倒计时
    _timerDown: function() {
        var time = this.data.time;

        --time;
        this.setData({
            time
        });

        if (time == 0) {
            return;
        }
        setTimeout(function() {
            this._timerDown();
        }.bind(this), 1000);
    },
    //物理键返回的时候需要上一个页面的loginBack制成1;
    /**
    _loginBack: function(loginBack) {
        var prevPage = getCurrentPages()[getCurrentPages().length - 2];
        prevPage.setData({
            loginBack
        })
    }
    **/
}));